package com.hgk.wanwan.main.fragment.persentfragment.viewUI;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AnalogClock;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.hgk.common.common.ui.activity.BallTitleAcitivty;
import com.hgk.wanwan.main.fragment.persentfragment.alarmclock.AlarmActivity;

import java.util.Calendar;

import width.hgk.wanwan.R;

//可以在
public class Demo4Activity extends BallTitleAcitivty {

    private TextView mTvText;
    private EditText mEtText;
    private View mEmptyView;
    private Object softHight;
    private AutoCompleteTextView mAutoComplete;

    private AnalogClock mAnalogClock;
    private TextView mTvClock;
    private static final int GUINOTIFIER = 0X1234;
    private Calendar mCalendar;
    public int mMinutes;
    public int mHour;
    public int mCount;
    private Handler mHandler;
    private Thread mClockThread;

    private DatePicker mDpTime;
    private TimePicker mTpTime;
    private TextView mTvTime;
    private int mYear,mMouth,mDay,mHours,mMinute,mSecent;

    private final String[] strs = {"a","b","c","d","ab","ac","ad","ba","bc","bd","ca","cb",
            "cd","abc","acd","adb","bac", "bcd","bdd","bda","cdb","ddb","abcd","acdb",
            "adba","badd","bcad","baddd"};
    private Object currentTime;
    private Object clockTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_demo4);
        initView();
        getSoftHight();//获取屏幕数据函数（没有实现呢）
    }

    private void initView() {
        mEmptyView = findViewById(R.id.view_demo4_empty);
        mEtText = (EditText) findViewById(R.id.et_persent_demo_one);
        mTvText = (TextView) findViewById(R.id.tv_persent_demo_one);
        mAutoComplete = (AutoCompleteTextView) findViewById(R.id.actv_autocomplete_textview);
        ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,strs);
        mAutoComplete.setAdapter(myArrayAdapter);
//        mEtText.setOnKeyListener(new View.OnKeyListener() {
//            @Override
//            public boolean onKey(View v, int keyCode, KeyEvent event) {
//                mTvText.setText(mEtText.getText().toString());
//                return false;
//            }
//        });
        mEtText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus == true) {
                    mEtText.setHint("请输入内容");
                }
            }
        });
        mEtText.addTextChangedListener(new TextWatcher() {//最好的方法
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mTvText.setText(mEtText.getText());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        /*时间钟表显示*/
        getClockTime();
        /**选择日期，时间显示*/
        getCurrentTime();

    }

    /**选择日期，时间显示*/
    public void getCurrentTime() {
        mDpTime = (DatePicker) findViewById(R.id.dp_demo4);
        mTpTime = (TimePicker) findViewById(R.id.tp_demo4);
        mTvTime = (TextView) findViewById(R.id.tv_time_show);
        Calendar cd = Calendar.getInstance();
        mYear = cd.get(Calendar.YEAR);
        mMouth = cd.get(Calendar.MONTH);
        mDay = cd.get(Calendar.DAY_OF_MONTH);
        mHours = cd.get(Calendar.HOUR_OF_DAY);
        mMinute = cd.get(Calendar.MINUTE);
        mSecent = cd.get(Calendar.SECOND);
        updataDisplay();
        mDpTime.init(mYear, mMouth, mDay, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMouth = monthOfYear;
                mDay = dayOfMonth;
                updataDisplay();
            }
        });
        mTpTime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                mHours = hourOfDay;
                mMinute = minute;
                //调用updataDisplay改变时间
                updataDisplay();
            }
        });
    }

    //选择时间的方法
    private void updataDisplay() {
        mTvTime .setText(new StringBuilder().append(mYear).append("/")
                .append(format(mMouth+1)).append("/")
                .append(format(mDay)).append("  ")
                .append(format(mHours)).append(":")
                .append(format(mMinute)).append(":")
                .append(format(mSecent)));
    }

    //日期时间显示两位数方法
    private String format(int x){
        String s = ""+x;
        if (s.length()==1){
            s = "0"+ s;
        }
        return s;
    }

    public void getClockTime() {
        mAnalogClock = (AnalogClock) findViewById(R.id.alc_analog_clock);
        mTvClock = (TextView) findViewById(R.id.tv_my_clock);
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {

                switch (msg.what){//这里是处理信息的方法
                    case Demo4Activity.GUINOTIFIER:
                        //在这里处理要TextView 对象show的事件
                        mTvClock.setText(format(mHour)+":"+format(mMinutes)+":"+format(mCount));
                        break;
                }
                super.handleMessage(msg);
            }
        };
        //利用进程来持续取得时间
        mClockThread = new LooperThread();
        mClockThread.start();
    }

    @Override
    public void setLeftButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public void setRightButton(RelativeLayout relativeLayout, Button button) {
        button.setVisibility(View.VISIBLE);
        button.setLayoutParams(new RelativeLayout.LayoutParams(55,45));
        button.setBackgroundResource(R.drawable.alarm1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlarmActivity.launch(mContext);
            }
        });
    }

    @Override
    public Object getTitleString() {
        return "我的主页";
    }

    @Override
    public int getBodyView() {
        return R.layout.activity_demo4;
    }

    //该洗一个ThreadClass 来持续获得时间
    class LooperThread extends Thread{
        @Override
        public void run() {
            super.run();
            try {
                do {
                    //取得系统时间
                    long time = System.currentTimeMillis();
                    //通过Calendar对象取得小时和分钟
                    final Calendar mCalendar = Calendar.getInstance();
                    mCalendar.setTimeInMillis(time);
                    mHour = mCalendar.get(Calendar.HOUR);
                    mMinutes = mCalendar.get(Calendar.MINUTE);
                    mCount = mCalendar.get(Calendar.SECOND);
                    Thread.sleep(1000);//让进程休息一秒
                    Message m = new Message();
                    m.what = Demo4Activity.GUINOTIFIER;
                    Demo4Activity.this.mHandler.sendMessage(m);
                } while (Demo4Activity.LooperThread.interrupted() == false);//当系统发出终端信息时停止本循环
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }



    public static void launch(Context context){
        context.startActivity(new Intent(context,Demo4Activity.class));
    }

    public void getSoftHight() {//获取软键盘高度（么有搞明白）
//        final Context context = getApplicationContext();
//        final RelativeLayout parentLayout = (RelativeLayout) findViewById(R.id.parent);
//        parentLayout.getViewTreeObserver(). addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                Rect r = new Rect();
//                // r will be populated with the coordinates of your view that area still visible.
//                parentLayout.getWindowVisibleDisplayFrame(r);
//                int screenHeight = myLayout.getRootView().getHeight();
//                int heightDiff = screenHeight - (r.bottom - r.top);
//                if (heightDiff > 100) {
//                    // if more than 100 pixels, its probably a keyboard
//                    // get status bar height
//                    int statusBarHeight = 0;
//                    try {
//                        Class<?> c = Class.forName("com.android.internal.R$dimen");
//                        Object obj = c.newInstance();
//                        Field field = c.getField("status_bar_height");
//                        int x = Integer.parseInt(field.get(obj).toString());
//                        statusBarHeight = context.getResources().getDimensionPixelSize(x);//获取屏幕的高度
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                    int realKeyboardHeight = heightDiff - statusBarHeight;
//                    Log.i("TAG", "keyboard height = " + realKeyboardHeight);
//                }
//            }
//        });
    }
}
